package com.lrkj.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.Const;
import com.lrkj.back.entity.ZsDabao;
import com.lrkj.back.service.ZsDabaoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.cbons.mamisay.parent.exception.BusinessException;

import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/packageing")
public class ZsDabaoController extends BaseController{
	@Autowired
	private ZsDabaoService dabaoService;
	
	//配置--包装信息
	@RequestMapping("/packageingList.do")
	public ModelAndView packageingList(String searchTxt,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<String,Object>();
		if(StringUtils.isNotBlank(searchTxt)){
			map.put("dabaoName", searchTxt.trim());
		}
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<ZsDabao> page = dabaoService.findAllDabao(map);
		request.setAttribute("dabaoList", page.getList());
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPages());
		return new ModelAndView("/peizhi/baozhuangxinxi/packageing_list");
	}
	//点添加和修改按纽用到的方法
	@RequestMapping("/packageingEdit.do")
	public ModelAndView packageingEdit(Integer id, HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		ZsDabao dabao = (id == null) ? new ZsDabao() : dabaoService.selectByPrimaryKey(id);
		request.setAttribute("dabao", dabao);
		return new ModelAndView("/peizhi/baozhuangxinxi/packageing_edit");
	}
	//修改和保存的方法
	@RequestMapping("/packageingSave.do")
	@ResponseBody
	public Map<Object,Object> packageingSave(ZsDabao dabao,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		try {
			dabao.setTbCreatetime(new Date());
			if(dabao.getDabaoId() != null){
				dabaoService.updateByPrimaryKey(dabao);
			}else{
				dabaoService.insert(dabao);
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	@RequestMapping("/packageingDel.do")
	@ResponseBody
	public Map<Object,Object> packageingDel(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		try {
			String ids = request.getParameter("id");
			String[] arraId = ids.split(",");
			for(String tid : arraId){
				System.out.println("+++++++"+tid);
				dabaoService.deleteByPrimaryKey(Integer.valueOf(tid));
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "删除成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "删除失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	
	
	
}
